package src.common;

import src.User.User;
import src.common.impl.Message;

import java.sql.*;
import java.util.Date;

public class DBUtil {

    public static String url = "jdbc:mysql://localhost:3306/yychat?useUnicode=true&characterEncoding=utf8";
    public static String db_username = "root";
    public static String db_password = "root";
    public static Connection conn = getConnection();

    private static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, db_username, db_password);
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    public static boolean seekUser(String username){
        boolean success = false;
        String user_query_str = "select * from user where username=?";
        PreparedStatement psmt ;
        try {
            psmt = conn.prepareStatement(user_query_str);
            psmt.setString(1, username);
            ResultSet rs = psmt.executeQuery();
            success = rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return success;
    }

    public static int registerUser(String username, String password){
        int cnt = 0;
        String user_insert_str = "insert into user(username,password) values(?,?)";
        PreparedStatement psmt ;
        try {
            psmt = conn.prepareStatement(user_insert_str);
            psmt.setString(1, username);
            psmt.setString(2, password);
            cnt = psmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return cnt;
    }


    public static boolean loginValidate(String username, String password){
        boolean login_flag = false;
        try {
            conn = DriverManager.getConnection(url, db_username, db_password);
            String user_query_str = "select * from user where username=? and password=?";
            PreparedStatement psmt = conn.prepareStatement(user_query_str);
            psmt.setString(1, username);
            psmt.setString(2, password);
            ResultSet rs = psmt.executeQuery();
            login_flag = rs.next();

        }catch (Exception e){
            e.printStackTrace();
        }
        return login_flag;
    }

    public static boolean loginByPhone(String phone, String password){
        boolean login_flag = false;
        try {
            conn = DriverManager.getConnection(url, db_username, db_password);
            String user_query_str = "select * from user where phone=? and password=?";
            PreparedStatement psmt = conn.prepareStatement(user_query_str);
            psmt.setString(1, phone);
            psmt.setString(2, password);
            ResultSet rs = psmt.executeQuery();
            login_flag = rs.next();

        }catch (Exception e){
            e.printStackTrace();
        }
        return login_flag;
    }


    public static String seekAllFriends(String username,int FriendType){
        String allFriends = "";
        String query_all_friends = "select slave_user from user_relation where master_user=? and relation=?";
        PreparedStatement psmt ;
        try {
            psmt = conn.prepareStatement(query_all_friends);
            psmt.setString(1,username);
            psmt.setInt(2,FriendType);
            ResultSet rs = psmt.executeQuery();
            while (rs.next()){
                allFriends = allFriends+" "+ rs.getString(1);
            }
            System.out.println(username+"全部好友"+allFriends);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return allFriends;
    }

    public static boolean seekFriend(String sender, String newFriend, int friendType) {
        boolean success = false;
        String query_user_relation_str = "select * from user_relation where master_user=? and slave_user = ? and relation=?";
        PreparedStatement psmt ;
        try {
            psmt = conn.prepareStatement(query_user_relation_str);
            psmt.setString(1,sender);
            psmt.setString(2,newFriend);
            psmt.setInt(3,friendType);
            ResultSet rs = psmt.executeQuery();
            success = rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return success;
    }

    public static int addFriend(String sender, String newFriend, int friendType) {
        int cnt = 0;
        String insert_user_into_user_relation_str = "insert into user_relation(master_user, slave_user, relation) values (?,?,?)";
        PreparedStatement psmt ;
        try {
            psmt = conn.prepareStatement(insert_user_into_user_relation_str);
            psmt.setString(1,sender);
            psmt.setString(2,newFriend);
            psmt.setInt(3,friendType);
            cnt = psmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return cnt;
    }

    public static void saveMessage(Message mess) {
        String insert_msg_str = "insert into message(sender, receiver, content, sendtime) values (?,?,?,?)";
        PreparedStatement psmt ;
        try{
            psmt = conn.prepareStatement(insert_msg_str);
            psmt.setString(1,mess.getSender());
            psmt.setString(2,mess.getReceiver());
            psmt.setString(3,mess.getContent());
            Date sendTime = mess.getSendTime();
            psmt.setTimestamp(4,new Timestamp(sendTime.getTime()));
            psmt.execute();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static void showAllMsg(String sender,String receiver){
        String show_message_str = "select * from message where sender=? and receiver=?";
        PreparedStatement psmt ;
        try{
            psmt = conn.prepareStatement(show_message_str);
            psmt.setString(1,sender);
            psmt.setString(2,receiver);;
            psmt.execute();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static void getUserNameByPhone(String phone, String password, User user) {
        String name = "";
        try {
            conn = DriverManager.getConnection(url, db_username, db_password);
            String query_username_by_phone = "select username from user where phone=? and password=?";
            PreparedStatement psmt = conn.prepareStatement(query_username_by_phone);
            psmt.setString(1, phone);
            psmt.setString(2, password);
            ResultSet resultSet = psmt.executeQuery();
            while (resultSet.next()){
                name = resultSet.getString("username");
            }
            user.setUsername(name);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
